const log4js = new require('log4js');
const httpServer = new require('http');
const querystring = new require('querystring');

log4js.configure({
    appenders: { cheese: { type: "file", filename: "cheese.log" } },
    categories: { default: { appenders: ["cheese"], level: "debug" } }
  });
const logger = log4js.getLogger("cheese");

const myServer = httpServer.createServer((request,response) =>{
    //post
    logger.debug('request是:'+request);
    let data = '';
    request.on('data',(chunk)=>{
        console.log(chunk);//<Buffer 75 6e 61 6d 65 3d 64 61 66 64 26 70 77 64 3d 33 31 34 33 31>
        console.log('数据流是'+chunk); //数据流是uname=dafd&pwd=31431(十六进制流被转化了)
        data += chunk;
    })
    request.on('end',()=>{
        //监听结束后返回相应的数据
        response.writeHead(200,{
            'content-type':'application/json;charset:utf-8',
        });
        console.log(data); //uname=dafd&pwd=31431
        console.log(querystring.parse(data));//使用parse方法将查询字符串转化为一个对象
        //由于响应头的格式是json格式，所以要将对象再转化为字符串
        console.log(JSON.stringify(querystring.parse(data)));

        response.write(JSON.stringify(querystring.parse(data)));
        response.end();
    })



    //get  下面的获取不到数据
    // logger.debug('request是:'+request);
    // let data = '';
    // request.on('data',(chunk) => {
    //     console.log('数据流是'+chunk);
    // })
    // request.on('end',(param1) => {
    //     response.writeHead('200',{
    //         'content-type':'application/json;charset=utf-8',
    //     })
    //     response.write('qwertyuio[');
    //     response.end();
    // })
});










myServer.listen(8888,'localhost',()=>{
    console.log('listening');
});







